package common;

import entity.Commodity;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class WebUtils {
    public static Commodity copyParamToBean(HttpServletRequest request, Commodity commodity) throws NullPointerException {
        if (request.getParameter("cId").equals("") || Objects.equals(request.getParameter("cName"), "") || request.getParameter("cPrice").equals("")) {
            throw new NullPointerException("有未填写的参数");
        } else {
            commodity.setcId(request.getParameter("cId"));
            commodity.setcName(request.getParameter("cName"));
            Enum<CommodityKind> kindEnum = CommodityKind.find(request.getParameter("cKind"));
            commodity.setcKind(kindEnum);
            commodity.setcPrice(Float.parseFloat(request.getParameter("cPrice")));
            return commodity;
        }
    }
}
